package edu.egypt.sdcard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SDCardSampleActivity extends Activity {
    private EditText editText;
	private Button loadButton;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText = (EditText) findViewById(R.id.editText1);
        loadButton = (Button)findViewById(R.id.button1);
        loadButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				File sdCard = Environment.getExternalStorageDirectory();
				File directory = new File(sdCard.getAbsolutePath()+"/ourDirectory");
				File targetFile = new File(directory,"egemen tanin.txt");
				
				//reading from the file
				try {
					FileInputStream inputStream = new FileInputStream(targetFile);
					InputStreamReader reader = new InputStreamReader(inputStream);
					//start reading
					char[] buffer = new char[100];
					StringBuffer s = new StringBuffer();
					int charRead;
					while((charRead = reader.read(buffer)) > 0){
						s.append(String.copyValueOf(buffer,0,charRead));
						buffer = new char[100];
					}
					editText.setText(s.toString());
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		});
    }
    
    
}